<template>
  <div>
    <el-input v-model="name" disabled>
      <el-button slot="append" icon="el-icon-s-grid" @click="openModal" />
    </el-input>

    <modal ref="modal" @ok="handleOK" />
  </div>
</template>

<script>
import Modal from './modal'
export default {
  name: 'UserGroupMultipleSelect',
  components: {
    Modal
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: '',
      required: false
    },
    width: {
      type: Number,
      default: 500,
      required: false
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    // 绑定对象，用于父组件动态创建多个选择框的情况
    refObject: {
      type: Object,
      default: null,
      required: false
    }

  },
  data() {
    return {
      visible: false,
      name: ''
    }
  },
  watch: {
    value: {
      immediate: true,
      handler: 'handleValue'
    }
  },

  methods: {
    openModal() {
      this.$refs.modal.show(this.value)
    },
    handleValue() {
      if (this.value) {
        const idList = this.value.split(',')
        this.$api.system.userGroup.getByIdList({ idList: idList })
          .then((res) => {
            this.name = res.data.map(x => x.name).join(',')
          })
      }
    },
    handleOK(value) {
      // 处理父组件绑定值
      this.$emit('change', value.join(','))
      // 将组织机构标识和名称传递回父组件
      this.$emit('changeSelect', value, this.refObject)
    }
  }
}
</script>

<style scoped>
</style>
